<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        header("Content-type: text/html; charset=utf-8");
        $this->session->set_userdata(array('user_lang' => 'portuguese'));
        $this->lang->load('global', $this->session->userdata['user_lang']);
    }

    protected function check_authentication($idtype = null, $no_redirect = false)
    {
		$is_logged = $this->is_logged();
		
		if (!$is_logged)
		{
			$this->load->helper('cookie');
			$member_data = unserialize(get_cookie('persistent'));
			
			if ($member_data)
			{
				$this->load->model('Membership_login_model', 'member_login');
				$last_login = $this->member_login->get_last_login($member_data);
				
				if ($last_login->cookie == $member_data['pass'])
				{
					load_session($member_data);
					$is_logged = true;
				}
			}
		}
		
		if ($is_logged)
		{
			$member_type = $this->session->userdata['idtype'];
			
			if (!$no_redirect && (!$idtype || $member_type != $idtype))
			{
				redirect(base_url() . $this->get_profile_type($this->session->userdata['idtype']));
			}
		}
		elseif ($this->uri->uri_string())
		{
			redirect(base_url() . 'login');
		}
    }
    
    protected function load_session($member_data)
    {
    	switch ($member_data['idtype'])
    	{
			case 2:
				$this->load->model('Company_model', 'company');
				$profile_data = $this->company->get_profile_data(array('idmember' => $member_data['idmember']));
				break;
			case 3:
				$this->load->model('Education_model', 'education');
				$profile_data = $this->education->get(array('idmember' => $member_data['idmember']));
				break;
			case 1:
			default:
				$this->load->model('Candidate_model', 'candidate');
				$profile_data = $this->candidate->get_profile_data(array('idmember' => $member_data['idmember']));
				break;
		}
		
		if ($member_data['idtype'] == 1)
		{
			$name = $profile_data->firstname . ' ' . $profile_data->lastname;
			$this->session->set_userdata(array('firstname' => $profile_data->firstname, 'lastname' => $profile_data->lastname));
		}
		else
		{
			$name = $profile_data->name;
		}
		
		$this->session->set_userdata(
			array(
				'logged' => true,
				'name' => $name,
	  			'idmember' => $member_data['idmember'],
	  			'idtype' => $member_data['idtype'],
				'idprofile' => $profile_data->id,
				'key' => $profile_data->key,
				'thumbnail' => $profile_data->thumbnail
			)
		);
		
		redirect(base_url() . $this->get_profile_type($member_data['idtype']));
    }
    
    public function get_profile_type($idtype = null)
    {
    	if (!$idtype)
    	{
    		$idtype = $this->session->userdata['idtype'];
    	}
    	
    	switch ($idtype)
    	{
    		case 2:
    			$profile_name = 'company';
    			break;
    			
    		case 3:
    			$profile_name = 'education';
    			break;
    		
    		default:
    		case 1:
    			$profile_name = 'candidate';
    			break;
    	}
    	
    	return $profile_name;
    }
    
	public function is_logged()
    {
    	return isset($this->session->userdata['logged']) && $this->session->userdata['logged'] == true;
    }
    
    public function get_subscription_menu(&$data, $subscription_id = null, $candidate_id = null)
    {
    	if (!$subscription_id && isset($this->session->userdata['idsubscription']))
		{
			$subscription_id = $this->session->userdata['idsubscription'];
		}
		
		if (isset($subscription_id) && is_numeric($subscription_id) && is_numeric($candidate_id))
		{
			$this->load->model('Subscription_model', 'subscription');
			$subscription = $this->subscription->get(array('idsubscription' => $subscription_id, 'idcandidate' => $candidate_id))->row();
			
			if ($subscription)
			{
				switch ($subscription->idstatus)
				{
					case 3:
						$status = $this->lang->line('profile_assessment');
						break;
					case 4:
						$status = $this->lang->line('profile_finalist');
						break;
					case 5:
						$status = $this->lang->line('profile_discarded');
						break;
					case 6:
						$status = $this->lang->line('profile_engaged');
						break;
					case 1:
					case 2:
					default:
						$status = $this->lang->line('profile_candidate');
						break;
				}
				
				$this->session->set_userdata(array('idsubscription' => $subscription_id));
				$data['header_base'] = '<div>
				<span class="highlighttext">'.sprintf($this->lang->line('profile_candidateas'), $status, $subscription->title).'.';
				
				$buttons = '';
				
				switch ($subscription->idstatus)
				{
					case 3:
						$buttons .= '<button type="button" onclick="checkSubscriptionStatus('.$subscription_id.', 4);" class="submit">'.$this->lang->line('profile_finalist').'</button>';
						break;
						
					case 4:
						$buttons .= '<button type="button" onclick="checkSubscriptionStatus('.$subscription_id.', 6);" class="submit">'.$this->lang->line('profile_engaged').'</button>';
						break;
					
					case 1:
					case 2:
					default:
						$buttons .= '<button type="button" onclick="checkSubscriptionStatus('.$subscription_id.', 3);" class="submit">'.$this->lang->line('profile_assessment').'</button>';
						break;
				}
				
				$buttons .= '<button type="button" onclick="checkSubscriptionStatus('.$subscription_id.', 5);" class="submit">'.$this->lang->line('profile_discarded').'</button>';
				
				if ($subscription->idstatus < 5)
				{
					$data['header_base'] .= ' ' . $this->lang->line('profile_checkcandidate') . ': ' . $buttons;
				}
				
				$data['header_base'] .= '</span></div>';
			}
		}
    }
    
    public function get_member_link($member_id = null, $member_type = null, $return_image = false, $subscription_id = null, $job_id = null)
    {
    	$this->lang->load('message', $this->session->userdata['user_lang']);
    	$link = '';
    	
    	switch ($member_type)
		{
			case 1:
				$this->load->model('Candidate_model', 'candidate');
				$candidate_data = $this->candidate->get_profile_data(array('idmember' => $member_id));
				$candidate_link = '<a href="' . base_url() . 'profile/candidate/'.$candidate_data->id.'/'.($candidate_data->firstname .'-' .$candidate_data->lastname).'/'.$subscription_id.'">%s</a>';
				
				if ($return_image === 'image')
				{
					$candidate_image = $candidate_data->image ? ($candidate_data->idmember.'/tmb/'.$candidate_data->image) : 'src/anonymous.png';
					$link .= '<div class="message-image">' . sprintf($candidate_link, '<img src="'.base_url().'f/'.$candidate_image.'" class="vmiddle" />') . '</div>';
				}
				elseif ($return_image)
				{
					$candidate_image = $candidate_data->thumbnail ? ($candidate_data->idmember.'/tmb/'.$candidate_data->thumbnail) : 'src/anonymous_thumb.png';
					$link .= '<div class="message-image">' . sprintf($candidate_link, '<img src="'.base_url().'f/'.$candidate_image.'" class="vmiddle" />') . '</div>';
				}
				else
				{
					$link .= sprintf($candidate_link, $candidate_data->firstname . ' ' . $candidate_data->lastname);
				}
				
				break;
				
			case 2:
				$this->load->model('Company_model', 'company');
				$company_data = $this->company->get_profile_data(array('idmember' => $member_id));
				
				$company_link = '<a href="' . base_url() . 'profile/company/'.$company_data->id.'/'. $company_data->name .'">%s</a>';
				
				if ($return_image === 'image')
				{
					$company_image = $company_data->image ? ($company_data->idmember.'/tmb/'.$company_data->image) : 'src/anonymous.png';
					$link .= '<div class="message-image">' . sprintf($company_link, '<img src="'.base_url().'f/'.$company_image.'" class="vmiddle" />') . '</div>';
				}
				elseif ($return_image)
				{
					$company_image = $company_data->thumbnail ? ($company_data->idmember.'/tmb/'.$company_data->thumbnail) : 'src/anonymous_thumb.png';
					$link .= '<div class="message-image">' . sprintf($company_link, '<img src="'.base_url().'f/'.$company_image.'" class="vmiddle" />') . '</div>';
				}
				else
				{
					$link .= sprintf($company_link, $company_data->name);
				}
				break;
				
			case 4:
				if (is_numeric($job_id))
				{
					$this->load->model('Job_model', 'job');
					$job_data = $this->job->get(array('idjob' => $job_id))->row();
				}
				else
				{
					$this->load->model('Subscription_model', 'subscription');
					$job_data = $this->subscription->get(array('idsubscription' => $member_id))->row();
				}
				$link .= '<a href="' . base_url() . 'job/detail/'.$job_data->id.'/'.str_replace(' ', '-', $job_data->title).'">' . $job_data->title . '</a>';
				break;
		}
		
		return $link;
    }
}

?>